#coding=utf-8
import os, sys, subprocess
import urllib, re, time, socket
socket.setdefaulttimeout(10)

def get_taotao(qq):
  url = 'http://taotao.qq.com/vc1/qz_first_utf8/def?uin=%s'%qq
  try:
    data = urllib.urlopen(url).read()
    data = eval(re.search(r'{.*}', data, re.I|re.S).group())['items']
  except Exception, e:
    print qq
    print e
    return None
  if not data:return None
  data = data[0]
  return data['pubDate'], data['title']

class ProcessPidFile(object):
  def __init__(self, pidfile):  
    self.pidfile = pidfile
  def __enter__(self):
    open(self.pidfile, 'w').write(str(os.getpid()))
  def __exit__(self, type, value, traceback):
    os.remove(self.pidfile)
    
if __name__ == '__main__':
  pidfile = '/home/jay/py/taotao/g.pid'
  if os.path.exists(pidfile):
    pid = open(pidfile).read().strip()
    print pid
    ps = subprocess.Popen(['ps', pid], stdout=subprocess.PIPE).communicate()[0]
    if ps.find(pid) != -1:
      print ps
      print 'exit'
      sys.exit()
      
  with ProcessPidFile(pidfile) as f:
  
    os.environ['DJANGO_SETTINGS_MODULE'] = 'taotao.dbserver.settings'
    from taotao.dbserver.taotao.models import Message
    if Message.objects.all().count() == 0:
      i = 1000000
    else: 
      i  = int(Message.objects.order_by('-created_at')[0].qq)
    
    print i
    n = 200
    for j in range(n):
      i = i + 1
      data = get_taotao(str(i))
      if not data:continue
      print i, data[0], data[1]
      m = Message(qq=str(i), published_at=data[0], content=data[1])
      try:
        m.save()
      except Exception,e:
        print e
        continue
      time.sleep(0.1)
    
    
